package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ReportDTO;
import com.api.shopapi.service.ReportImgService;
import com.api.shopapi.service.ReportService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("Report")
@RestController
public class ReportController extends BaseController{

    @Autowired
    @Qualifier("ReportServiceImplOne")
    private ReportService reportService;

    @Autowired
    @Qualifier("ReportImgServiceImplOne")
    private ReportImgService reportImgService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    //上传检查报告
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Report/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            String hospitalName = jsonObject.getString("hospitalName");
            String inspectTimeStr = jsonObject.getString("inspectTime");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date inspectTime = sdf.parse(inspectTimeStr);
            JSONArray reportImgList = jsonObject.getJSONArray("reportImgList");

            Report report=new Report();
            report.setReportCode(UUID.randomUUID().toString());
            report.setCreateTime(new Date());
            report.setHospitalName(hospitalName);
            report.setInspectTime(inspectTime);
            report.setUserCode(userCode);

            reportService.insert(report,reportImgList);


            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/Report/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查看检查报告列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Report/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");

            PageInfo<Report> reportPageInfo = reportService.getPageList(pageIndex, pageSize, userCode);
            List<ReportDTO> reportDTOList=new ArrayList<>();
            for (Report report : reportPageInfo.getList()) {
                ReportDTO reportDTO = mapper.map(report, ReportDTO.class);
                List<ReportImg> reportImgList = reportImgService.getListByReportCode(report.getReportCode());
                reportDTO.setReportImgList(reportImgList);
                reportDTOList.add(reportDTO);
            }
            reportPageInfo.setList(Collections.unmodifiableList(reportDTOList));

            return success(reportPageInfo);
        }catch (Exception e){
            insertError("/Report/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查看某个政府下所有检查报告列表
    @RequestMapping(value = "GetPageByGovernment",method = RequestMethod.POST)
    public MyResult PageAdm(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Report/GetPageByGovernment");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userName = jsonObject.getString("userName");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");

            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();

            PageInfo<ReportDTO> reportDTOPageInfo = reportService.GetPageByGovernment(pageIndex, pageSize, userName, governmentCode);

            return success(reportDTOPageInfo);
        }catch (Exception e){
            insertError("/Report/GetPageByGovernment", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //查看某个检查报告的图片
    @RequestMapping(value = "GetPictureList",method = RequestMethod.POST)
    public MyResult GetPictureList(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Report/GetPictureList");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String reportCode = jsonObject.getString("reportCode");

            List<ReportImg> reportImgList = reportImgService.getListByReportCode(reportCode);
            return success(reportImgList);
        }catch (Exception e){
            insertError("/Report/GetPictureList", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
